/**
 * Created by linwei on 7/27/2016.
 */

$(document).ready(function () {
    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#pipeSegmentArr').select3({
        title: '管段'
    });
    // 起点位置
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        mileType: 'float',
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment}
    });
    // 终点位置
    var $end = $('#JQ_end').select3({
        title: '终点位置',
        server: true,
        mileType: 'float',
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment, 'beginSignId': $begin}
    });
    //触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin', true);
                var end = DyyBase.getPos(dataset, 'end', true);
                $begin.asset(begin);
                $end.asset(end);
            });

        } else {
            $segment.add([]);
            $begin.add([]);
            $end.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin', true);
                var end = DyyBase.getPos(dataset, 'end', true);
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $begin.add([]);
            $end.add([]);
        }
    });

    // 搜索条件
    var filter = {
        // 获得表单项
        getCondition: function(){
            var data = {};
            //管道名称
            data.pipeLineId=function(){
                var v = $pipeline.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.pipeName=function(){
                var v = $pipeline.val('text');
                if (v) {
                    return v;
                }
                return '';
            }();
            //起点绝对距离
            data.fromMileage=function(){
                var v= $begin.val('mile');
                if (DyyBase.notEmpty(v)){
                    return v;
                }
                return '';
            }();
            //终点绝对距离
            data.toMileage=function(){
                var v= $end.val('mile');
                if (DyyBase.notEmpty(v)){
                    return v;
                }
                return '';
            }();
            return data;
        },
        // 执行查询后的筛选对象
        getExportFilter: function(){
            return filter.getCondition();
        }
    };


    function risk_statistics() {
        var param = filter.getCondition();
        var url = DyyBase.root_path + '/riskMgr/riskAssessment/statistics.json';
        DyyBase.ajax(url, param, function(dataset){
            if(dataset.bool){
                var risk = dataset.result.riskAssessmentList;
                var percent = dataset.result.riskGradeList;
                if(risk.length > 0){
                    var segment_list = [];
                    var temp_list = [];
                    for(var j = 0, len = risk.length; j < len; j++){
                        if(risk[j]['evaluateName'] && risk[j]['evaluateScore']){
                            temp_list.push(risk[j]);
                        }
                    }
                    segment_list.push({x:0,y:'default'});
                    segment_list = segment_list.concat(d3Charts.formart(temp_list,{x:'evaluateScore',y:'evaluateName'}));
                    segment_list.push({x:0,y:'default'});
                    console.log(segment_list);

                    // 计算容器高度
                    var $box = $('#JQ_segment_grade');
                    var b_w = $box.width();
                    var b_h = 297;
                    if(segment_list.length > 8){
                        b_h = segment_list.length * 20;
                    }
                    $box.width(b_w);
                    $box.height(b_h).empty();

                    // 画图
                    d3Charts.barVx({
                        container: 'JQ_segment_grade',
                        color: ['#fe8e06'],
                        data: segment_list,
                        layout: {
                            xtag: '',                 // x轴显示文本
                            ytag: '',                 // y轴显示文本
                            margin: {
                                left: 190,
                                right: 40,
                                top: 30,
                                bottom: 30
                            },                           // 距容器边距
                            xgrid: false,                // x轴网格
                            ygrid: false                 // y轴网格
                        },
                        xAxis: {                         // x轴数据
                            data: [],
                            ticks: 10
                        },
                        yAxis: {                         // y轴数据
                            data: [],
                            ticks: 0,
                            render: function(d){
                                if(d != 'default'){
                                    var dy = d.length > 14 ? d.substr(0,14) + '...' : d;
                                    return dy;
                                }else{
                                    return '';
                                }
                            }
                        }
                    });
                }else{
                    $('#JQ_segment_grade').html('<img src="'+$('#JQ_segment_grade').attr('data-empty')+'">').height(297);
                }

                if(percent.length > 0){
                    var percent_data = new Array();
                    for (var i = 0; i < percent.length; i++) {
                        percent_data.push({name: percent[i].gradeName, value: percent[i].countNum});
                    }
                    new PicEChart().drwaHollowPie({
                        data: percent_data,
                        divObject: "riskGradeDiv",
                        colors: ["#fe8e07", "#ffc501", "#4cc9ce", "#61d2fe", "#37a4dd"],
                        centerx: "30%",
                        w:"50%",
                        h:"30%"
                    });
                }else{
                    $('#riskGradeDiv').html('<img src="'+$('#riskGradeDiv').attr('data-empty')+'">');
                }

                //
                // 最高危险管段
                $("#mostDangerousPipeSegment").html(dataset.result.mostDangerousPipeSegment == "[]" ? "无数据" : dataset.result.mostDangerousPipeSegment);
                // 高风险管段比例
                $("#highRiskProportion").html(dataset.result.highRiskProportion);
                // 最高风险管段评分
                $("#mostDangerousScore").html(dataset.result.mostDangerousScore);
                // 已采取措施管段
                $("#measurePipeSegment").html(dataset.result.measurePipeSegment);
            }
        });
    }

    risk_statistics();


    // 绑定查询按钮
    $("#JQ_search").on('click', function () {
        risk_statistics();
    });

    //导出
    $('#btn_export').on('click', function () {
        var param = filter.getCondition;
        if (param instanceof Function) {
            param = param();
        }
        $(this).EXPT({
            type: 'name',
            name: param.pipeName + '风险评价统计图',
            yesBtnAct: function (callback, fname) {
                callback();
                canvasPdf.exportHtmlPdf({
                    dom: $('#JQ_pdf_area'),
                    server: DyyBase.root_path + '/comm/downImgPdf.json',
                    param: {
                        fileName: fname,
                        name: param.pipeName + '风险评价统计图'
                    }
                });
            }
        });
    });
});

